﻿using Sunisoft.IrisSkin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace h1
{
    public partial class Form1 : Form
    {
        accout zidong;
        List<accout> accouts= new List<accout>();
        Dictionary<string , string> dic = new Dictionary<string , string>();
        public bool confirm;
        public Form1()
        {
            InitializeComponent();
        }

        //界面换肤
        public static Sunisoft.IrisSkin.SkinEngine skinEngine = new Sunisoft.IrisSkin.SkinEngine();
        public static List<string> Skins;
        
        private void Form1_Load(object sender, EventArgs e)
        {
            skinEngine.Active = false;
            skinEngine.SkinFile = System.Environment.CurrentDirectory + "\\Skins\\初音蓝.ssk";
            skinEngine.Active = true;

            accouts.Clear();
            dic.Clear();
            comboBox2.Items.Clear();
            comboBox1.SelectedIndex = 0;
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<accout>));
                string xmlFileName = "accoutData.xml";
                using (FileStream fs = new FileStream(xmlFileName, FileMode.Open))
                {
                    accouts = xmlSerializer.Deserialize(fs) as List<accout>;
                }
                foreach (var acc in accouts)
                {
                    if (acc.user == "" || acc.user == null) continue;
                    dic.Add(acc.accType.ToString() + "-" + acc.user, acc.password);
                }
                List<accout> accoutsTemp = (from accout in accouts
                                            where accout.accType == 0 && accout.user != "" && accout.password != ""
                                            select accout).ToList();
                foreach (accout acc in accoutsTemp)
                {
                    if (acc.user == "" || acc.user == null) continue;
                    comboBox2.Items.Add(acc.user);
                }
                if (accouts.Count > 0)
                {
                    button8.Visible = true;
                    comboBox2.SelectedIndex = 0;
                    textBox2.Text = dic["0-" + comboBox2.Text];
                }
            }catch(Exception ex)
            {
                Form_message fm = new Form_message("账号反序列化出错！", "出错描述：" + ex.Message);
                fm.ShowDialog();
            }
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(accout));
                string xmlFileName = "zidongData.xml";
                using (FileStream fs = new FileStream(xmlFileName, FileMode.Open))
                {
                    zidong = xmlSerializer.Deserialize(fs) as accout;
                }
                if (zidong != null && zidong.user != "")
                {
                    string item = (zidong.accType == 0) ? "userName" : "userID";
                    Form_list fl = new Form_list(this, $"{item}='{zidong.user}'");
                    fl.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                //Form_message fm = new Form_message("账号(自动登录)反序列化出错！", "出错描述：" + ex.Message);
                //fm.ShowDialog();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", @".\log.txt");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void denglu_cancelZidong()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(accout));
                string xmlFileName = "zidongData.xml";
                using (FileStream fs = new FileStream(xmlFileName, FileMode.Create))
                {
                    xmlSerializer.Serialize(fs, new accout(0, "", ""));
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("账号(取消自动登录)序列化错误", "错误描述：" + ex.Message);
                fm.ShowDialog();
            }
        }
        private void denglu_zidong()
        {
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(accout));
                string xmlFileName = "zidongData.xml";
                using (FileStream fs = new FileStream(xmlFileName, FileMode.Create))
                {
                    xmlSerializer.Serialize(fs, new accout(comboBox1.SelectedIndex, comboBox2.Text, textBox2.Text));
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("账号(自动登录)序列化错误", "错误描述：" + ex.Message);
                fm.ShowDialog();
            }
        }
        private void denglu_jizhu()
        {
            bool f1 = false;
            foreach (accout acc in accouts)
            {
                if (acc.accType == comboBox1.SelectedIndex && acc.user == comboBox2.Text)
                {
                    if (acc.password != textBox2.Text)
                    {
                        acc.password = textBox2.Text;
                        dic[comboBox1.SelectedIndex + "-" + comboBox2.Text] = textBox2.Text;
                    }
                    f1 = true; break;
                }
            }
            if (!f1)
            {
                accouts.Add(new accout(comboBox1.SelectedIndex, comboBox2.Text, textBox2.Text));
            }
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<accout>));
                string xmlFileName = "accoutData.xml";
                using (FileStream fs = new FileStream(xmlFileName, FileMode.Create))
                {
                    xmlSerializer.Serialize(fs, accouts);
                }
            }
            catch (Exception ex)
            {
                Form_message fm = new Form_message("账号序列化错误", "错误描述：" + ex.Message);
                fm.ShowDialog();
            }
            if (!dic.Keys.Contains(comboBox1.SelectedIndex + "-" + comboBox2.Text))
            {
                dic.Add(comboBox1.SelectedIndex + "-" + comboBox2.Text, textBox2.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox2.Text == "")
            {
                errorProvider1.SetError(comboBox2, "用户名/ID不能为空");
            }
            else if(textBox2.Text == "")
            {
                errorProvider1.SetError(textBox2, "密码不能为空");
            }
            else
            {
                errorProvider1.Clear();
                if(comboBox2.Text =="admin"&& textBox2.Text == "admin")
                {
                    Form_data form_Data = new Form_data();
                    form_Data.ShowDialog();
                }
                else
                {
                    string loginItem = "";
                    if (comboBox1.SelectedItem == "用户ID登陆")
                    {
                        loginItem = "userID";
                    }
                    else if (comboBox1.SelectedItem == "用户名登陆")
                    {
                        loginItem = "userName";
                    }
                    DataSet ds = MySqlHelper.getDataSet(MySqlHelper.Conn, $"SELECT {loginItem},userPassword FROM users WHERE {loginItem}='{comboBox2.Text}'");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        Form_message fm = new Form_message("登陆失败！", "该用户名/用户ID不存在！");
                        fm.ShowDialog();
                        return;
                    }
                    else if (ds.Tables[0].Rows[0]["userPassword"].ToString() != textBox2.Text)
                    {
                        Form_message fm = new Form_message("登陆失败！", "密码错误，请重新输入！");
                        fm.ShowDialog();
                        return;
                    }
                    else
                    {
                        if (checkBox2.Checked == true)
                        {
                            denglu_zidong();
                            denglu_jizhu();
                        }
                        else if (checkBox1.Checked == true)
                        {
                            denglu_jizhu();
                        }
                        try
                        {
                            this.Hide();
                            Form_message fm = new Form_message("登陆成功！", "开始安排你的行程吧！");
                            fm.ShowDialog();
                            Form_list fl = new Form_list(this, $"{loginItem}='{comboBox2.Text}'");
                            fl.ShowDialog();
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            Form_message fm = new Form_message("登录过程出错！", "出错描述：" + ex.Message);
                            fm.ShowDialog();
                        }
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileStream fout = new FileStream(@"log.txt", FileMode.Create, FileAccess.Write);
            StreamWriter bw = new StreamWriter(fout);
            bw.Write("");
            bw.Close();
            Form_message form_message = new Form_message("提示：" , "日志文件已清空！");
            form_message.ShowDialog();
            fout.Close();
        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            Form_register fr = new Form_register();
            fr.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if(button5.ImageIndex == 0) 
            { 
                button5.ImageIndex = 1;
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {
                button5.ImageIndex = 0;
                textBox2.UseSystemPasswordChar = true;
            }
        }
        public class accout
        {
            public accout()
            {
                accType = 0;
                user = "";
                password = "";
            }
            public accout(int type, string u,string p)
            {
                accType = type;
                user= u;
                password= p;
            }
            public int accType { get; set; }
            public string user { get; set; }
            public string password { get; set; }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            button8.Visible = false;
            List<accout> accoutsTemp = (from accout in accouts
                                        where accout.accType == comboBox1.SelectedIndex && accout.user != "" && accout.password != ""
                                        select accout).ToList();
            foreach (accout acc in accoutsTemp)
            {
                if (acc.user == "" || acc.user == null) continue;
                comboBox2.Items.Add(acc.user);
            }
            if(comboBox2.Items.Count > 0)
            {
                button8.Visible = true;
                comboBox2.SelectedIndex = 0;
                textBox2.Text = dic[comboBox1.SelectedIndex.ToString()+"-" + comboBox2.Text];
            }
            else
            {
                comboBox2.Text = "";
                textBox2.Text = "";
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.Items.Count > 0)
            {
                textBox2.Text = dic[comboBox1.SelectedIndex.ToString() + "-" + comboBox2.Text];
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Form_getUser fg = new Form_getUser(comboBox1.SelectedIndex,comboBox2.Text);
            fg.ShowDialog();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Form_confirm fc = new Form_confirm(this, $"正准备删除账户信息：\n{((comboBox1.SelectedIndex == 0) ? "用户名" : "用户ID")}-{comboBox2.Text}\n【注意】：删除后需要重新输入账号密码登陆！", "是否确认删除账号信息？");
            fc.ShowDialog();
            if (confirm)
            {
                accout temp = new accout();
                foreach(accout acc in accouts)
                {
                    if (acc.accType == comboBox1.SelectedIndex && acc.user == comboBox2.Text)
                    {
                        temp=acc;
                    }
                }
                if(accouts.Contains(temp))
                {
                    accouts.Remove(temp);
                }
                try
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<accout>));
                    string xmlFileName = "accoutData.xml";
                    using (FileStream fs = new FileStream(xmlFileName, FileMode.Create))
                    {
                        xmlSerializer.Serialize(fs, accouts);
                    }
                }
                catch (Exception ex)
                {
                    Form_message fm = new Form_message("账号序列化错误", "错误描述：" + ex.Message);
                    fm.ShowDialog();
                }
                comboBox2.Items.Clear();
                button8.Visible = false;
                List<accout> accoutsTemp = (from accout in accouts
                                            where accout.accType == comboBox1.SelectedIndex && accout.user != "" && accout.password != ""
                                            select accout).ToList();
                foreach (accout acc in accoutsTemp)
                {
                    if (acc.user == "" || acc.user == null) continue;
                    comboBox2.Items.Add(acc.user);
                }
                if (comboBox2.Items.Count > 0)
                {
                    button8.Visible = true;
                    comboBox2.SelectedIndex = 0;
                    textBox2.Text = dic[comboBox1.SelectedIndex.ToString() + "-" + comboBox2.Text];
                }
                else
                {
                    comboBox2.Text = "";
                    textBox2.Text = "";
                }
            }
        }

        
        
      
    }
}
